package sandbox.j2se;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class SmtpTest {

    public static void main(String[] args) {
        String host = args.length > 0? args[0]: "127.0.0.1";
        int port = args.length > 1? Integer.parseInt(args[1]): 12525;
        String user = args.length > 2? args[2]: "reestr112";
        String password = args.length > 3? args[3]: "P@$$w0rd";
        boolean tls = args.length > 4? Boolean.parseBoolean(args[4]): true;
        String to = args.length > 5? args[5]: "pmironenko@jet.msk.su";
        String from = args.length > 6? args[6]: "reestr112@yandex.ru";

        SmtpTest.sendMail(host, port, user, password, tls, to, from);
    }

    public static void test() {
//        new SmtpTest().sendMail("smtp.yandex.ru", 12525, "reestr112", "P@$$w0rd", true, "pmironenko@jet.msk.su", "info@mchs.gov.ru");
        SmtpTest.sendMail("localhost", 12525, "reestr112", "P@$$w0rd", true, "pmironenko@jet.msk.su", "reestr112@yandex.ru");
//        SmtpTest.sendMail("smtp.yandex.ru", 12525, "reestr112", "P@$$w0rd", true, "pmironenko@jet.msk.su", "reestr112@yandex.ru");
//        SmtpTest.sendMail("smtp.gmail.com", 12525, "pmironenko", "abitur2001", true, "pmironenko@jet.msk.su", "pmironenko@gmail.com");
    }

    private static void sendMail(String host, int port, final String user, final String pass, boolean tls, String recipient, String from) {
        try {
//            Properties properties = System.getProperties();
            Properties properties = new Properties();
//            properties.put("mail.transport.protocol", "smtp");
            properties.put("mail.smtp.host", host);
            properties.put("mail.smtp.port", port);
            properties.put("mail.debug", "true");
            properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            if(tls) {
                properties.put("mail.smtp.starttls.enable", "true");
            }
            Session session;
            if(user != null && pass != null) {
                properties.put("mail.smtp.auth", "true");
//                properties.put("mail.smtp.user", user);
//                properties.put("mail.smtp.password", pass);
//                properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//                session = Session.getDefaultInstance(properties);
                session = Session.getInstance(properties, new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, pass);
                    }
                });
            } else {
                session = Session.getDefaultInstance(properties);
            }
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipients(Message.RecipientType.TO, recipient);
            message.setSubject("mail to " + host + " test ~ " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            message.setText("test");

            Transport transport = session.getTransport("smtp");
            transport.connect(host, port, user, pass);
            Transport.send(message);
            System.out.println("Email with data was sent");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
